package demo2;

import java.util.Objects;

class Money implements Cloneable{
    public double money = 12.25;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student implements Cloneable{
    public String name;
    public Money m = new Money();//组合

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override//受保护的
    protected Object clone() throws CloneNotSupportedException {
        //只是克隆了Student对象
        Student student = (Student) super.clone();//深拷贝
        //克隆了Student对象里的Money对象
        student.m = (Money) this.m.clone();
        return student;
        //return super.clone();//调用父类的克隆方法
    }

    /*public boolean equals(Object obj) {//重写equals
        if (obj == null) {
            return false;
        }
        //指向的是同一个对象
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Student)) {
            return false;
        }
        Student student = (Student) obj;
        if (this.name.equals(student.name)) {
            return true;
        }else {
            return false;
        }
    }*/

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {//算对象的位置
        return Objects.hash(name);
    }
}
class Person {
}
class Animal {
}
class Dog {
    public void func1() {}
    public void func2() {}
    public void func3() {}
}

interface A {}
public class Test {
    public static void main(String[] args) {
        Student student = new Student();//向上转型
        student.name = "zhangsan";
        //我们希望这俩对象放到同一个位置去
        Student student1 = new Student();//向上转型
        student1.name = "zhangsan";

        System.out.println(student.hashCode());
        System.out.println(student1.hashCode());
    }
    public static void main4(String[] args) {
        Student student = new Student();//向上转型
        student.name = "zhangsan";

        Dog dog = new Dog();
        boolean flg = student.equals(dog);
        System.out.println(flg);
        /*Student student1 = student;*/

        /*Student student1 = new Student();
        student1.name = "zhangsan";*/

        /*System.out.println(student == student1);

        boolean flg = student.equals(student1);
        System.out.println("flg:" + flg);
        System.out.println(student);//动态绑定*/
    }
    public static void func(Object obj) {

    }

    public static void main3(String[] args) {
        func(new Money());//匿名对象 缺点：每次使用都得new
        func(new Person());
        func(new Animal());
        func(new Dog());
        Dog dog = new Dog();
        dog.func1();
        dog.func2();
        dog.func3();
        System.out.println("=====");
        //匿名对象 使用场景就是只用一次
        new Dog().func1();
        new Dog().func2();
        new Dog().func3();
    }
    public static void mai2n(String[] args) throws CloneNotSupportedException {
        Student student = new Student();
        Student student2 = (Student) student.clone();//向下转型
        System.out.println( student.m.money);
        System.out.println( student2.m.money);
        System.out.println("===============");
        student.m.money = 99;//浅拷贝
        System.out.println( student.m.money);
        System.out.println( student2.m.money);

        Object o = new Student();
    }

    public static void main1(String[] args) throws CloneNotSupportedException {
        Student student = new Student();
        student.name = "bitjiuyeke";
        Student student2 = (Student) student.clone();//向下转型
        System.out.println(student);
        System.out.println(student2);
    }
}
